package com.trkj.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.IHwcPowerDAO;
import com.trkj.dao.jpa.IHwcPowersDAO;
import com.trkj.dao.jpa.IHwcStaffPowerDAO;
import com.trkj.pojos.Tbpower;
import com.trkj.pojos.Tbstaffpower;

@Transactional
@Service
public class HwcPowerService {
	
	@Autowired
	private IHwcStaffPowerDAO spDAO;	//员工权限表
	
	@Autowired
	private IHwcPowerDAO powerDAO;
		/**
		 * 根据传入的员工编号查询权限对象并显示
		 * @param staffNo
		 * @return
		 */	
		public List<Tbpower> queryStaffNoByPowers(long staffNo){
			return powerDAO.queryStaffNoByPowers(new BigDecimal(staffNo));
		}
		/**
		 * 根据传入的员工编号查询不相关权限对象并显示
		 * @param staffNo
		 * @return
		*/
		public List<Tbpower> queryStaffNoByNotPowers(long staffNo){
			return powerDAO.queryStaffNoNotPowers(new BigDecimal(staffNo));
		}
		/**
		 * 根据传入的字符查询不属于该员工的权限
		 * @param staffNo
		 * @param powerGrade
		 * @return
		 */
		public List<Tbpower> queryNotStaffNoByNotPowers(long staffNo,String powerGrade){
			return powerDAO.queryNotStaffNoNOtPowers(new BigDecimal(staffNo), powerGrade);
		}
		/**
		 * 根据传入字符模糊查询属于该员工的权限
		 * @param staffNo
		 * @param powerGrade
		 * @return
		 */
		public List<Tbpower> queryPowerNameNoByPowers(long staffNo,String powerGrade){
			return powerDAO.queryStaffNoByPowerGrade(new BigDecimal(staffNo), powerGrade);
		}
		/*
		//获取权限编号集合
		List<BigDecimal> list=spDAO.queryStaffNoByPowers(new BigDecimal(staffNo));
		//创建一个新的集合去接受查询出来的集合
		List<Tbpower> power=new ArrayList<Tbpower>();
		for(BigDecimal powersNo :list){
			Tbpower tp=powerDAO.findOne(powersNo.longValue());
			power.add(tp);
		}
		return power;
	}*/
	/**
	 * 添加员工权限
	 * @param powerNos
	 * @param staffNo
	 */
	public void addStaffPowers(Long[] powerNos,BigDecimal staffNo){
		//将数组转换成集合
		DefaultConversionService service = 
				new DefaultConversionService();
		List<Long> list = service.convert(powerNos, List.class);
		//循环添加数据
		for(long l : list){
			//System.out.println("添加权限Service \t 员 工编号"+staffNo+"\n 权限编号"+l);
			Tbstaffpower sp = new Tbstaffpower();
			sp.setPowersNo(new BigDecimal(l));
			sp.setStaffNo(staffNo);
			spDAO.save(sp);
		}	
	}
	
	/**
	 * 移除员工的权限
	 * @param powerNos
	 * @param staffNo
	 */
	public void moveStaffPowers(Long[] powerNos,BigDecimal staffNo){
		DefaultConversionService service = 
				new DefaultConversionService();
		List<Long> list = service.convert(powerNos, List.class);
		
		for(long l :list){
			
			Tbstaffpower sp = spDAO.findPowerStaffSP(new BigDecimal(l), staffNo);
			//System.err.println("删除权限对象编号"+sp.getStaffpowersNo());
			spDAO.delete(sp);
			
		}
	}
	/**
	 * 传入账号查询权限集合
	 * @param account
	 * @return
	 */
	public List<Tbpower> findStaffNOBypowersNo(String account){
		//从账号中截取编号
		long staffNo = Long.parseLong(account.substring(6));
		//System.out.println("Service 传入账号查询集合  staffNo" +staffNo +" account"+account);
		return powerDAO.findAccountBystaffPowers(new BigDecimal(staffNo));	
	}
	
}
